﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MotorcycleStore.Repository;
using MotorcycleStore.DomainModel;

namespace MotorcycleStoreData.EF
{
	public class MotorcycleRepository : IMotorcycleRepository
	{
		private MotorcycleStoreContext context;

		public MotorcycleRepository(string connectionString)
		{
			context = new MotorcycleStoreContext(connectionString);
		}

		public IQueryable<Manufacturer> Manufacturers
		{
			get
			{
				return context.Manufacturers;
			}
		}

		public IQueryable<Motorcycle> Motorcycles
		{
			get
			{
				return context.Motorcycles
					.Include("Manufacturer")
					.Include("Categories");
			}
		}

		public IQueryable<Category> Categories
		{
			get
			{
				return context.Categories;
			}
		}

		public void Add(Motorcycle motorcycle)
		{
			context.Motorcycles.AddObject(motorcycle);
		}

		public void Delete(int motorcycleId)
		{
			var motorcycle = context.Motorcycles.Single(m => m.Id == motorcycleId);
			context.DeleteObject(motorcycle);
		}

		public void Save()
		{
			context.SaveChanges();
		}
	}
}
